﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace BEELab
{
    public partial class LiveData : System.Web.UI.Page
    {
        DataTable result = new DataTable();

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink link = new HyperLink();
                link.Text = e.Row.Cells[0].Text;
                link.NavigateUrl = "./Logic/Execution/Execute.aspx?SubjectID=" + e.Row.Cells[0].Text;
                link.Target = "_blank";
                e.Row.Cells[0].Controls.Add(link);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null && Session["UserID"] != null && BEELab.Logic.ProgramManagement.SessionManagement.SessionManagement.GetOwnerID(int.Parse(Request.QueryString["ID"].ToString())) != int.Parse(Session["UserID"].ToString()) && (Session["Admin"] == null || (bool)Session["Admin"] == false))
            {
                Response.Redirect("Main.aspx");
            }

            if (Request.QueryString["ID"] != null)
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT SubjectID, Label, Value FROM RecordedData JOIN Subjects ON Subjects.ID=RecordedData.SubjectID WHERE Subjects.SessionID=@SessionID";
                command.Parameters.Add(new SqlParameter("@SessionID", int.Parse(Request.QueryString["ID"].ToString())));
                DataTable data = BEELab.Data.DBHelper.ExecuteQuery(command);

                command = new SqlCommand();
                command.CommandText = "SELECT ID FROM Subjects WHERE SessionID=@SessionID";
                command.Parameters.Add(new SqlParameter("@SessionID", int.Parse(Request.QueryString["ID"].ToString())));
                DataTable subjectIDs = BEELab.Data.DBHelper.ExecuteQuery(command);

                result.Columns.Add("SubjectID");


                DataRow newRow;
                DataRow[] rows;

                foreach (DataRow row in subjectIDs.Rows)
                {
                    rows = data.Select("SubjectID='" + row["ID"].ToString() + "'");
                    foreach (DataRow element in rows)
                    {
                        if (!result.Columns.Contains(element["Label"].ToString()))
                        {
                            result.Columns.Add(element["Label"].ToString());
                        }
                    }

                    newRow = result.NewRow();
                    newRow["SubjectID"] = row["ID"].ToString();
                    foreach (DataRow element in rows)
                    {
                        newRow[element["Label"].ToString()] = element["Value"].ToString();
                    }

                    result.Rows.Add(newRow);
                }

                GridView1.DataSource = result;
                GridView1.DataBind();
            }
        }
    }
}